﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using Quizzer.Model;
using Quizzer.DataAccess;
using System.Collections.ObjectModel;

namespace Quizzer.ViewModel
{
    [Magic]
    public class CoursesViewModel : ViewModelBase
    {
        public CoursesViewModel()
        {
            Messenger.Default.Register<Account>(this, ReceiveMessage);

            GoToProfilePage = new RelayCommand(() => ExecuteGoToProfilePage());
            GoToTestPage = new RelayCommand(() => ExecuteGoToTestPage(), ()=>SelectedTest != null);
            BeginTest = new RelayCommand(() => ExecuteBeginTest(), ()=>SelectedTest != null);
        }

        private void ReceiveMessage(Account acc)
        {
            _account = acc;
            ProfileName = _account.Username;
            ProfileImage = _account.Avatar;

            CoursesList = CourseRepository.GetCourses(_account);

            ProfileProperty1 = "Statistics: 90%"; 
            ProfileProperty2 = "Done: 10";
            ProfileProperty3 = "Succeed: 9";
        }
        
        #region Fields

        private Account _account;
        private Course _selectedCourse;

        #endregion

        #region Properties

        [NoMagic]
        public Course SelectedCourse
        {
            get { return _selectedCourse; }
            set
            {
                if (_selectedCourse != value)
                {
                    _selectedCourse = value;

                    TestList = TestRepository.GetTestsOfCourse(SelectedCourse);
                    
                    RaisePropertyChanged("SelectedCourse");
                }
            }
        }
        public Test SelectedTest { get; set; }

        public ObservableCollection<Course> CoursesList { get; set; }
        public ObservableCollection<Test> TestList { get; set; }

        public string ProfileName { get; set; }
        public Image ProfileImage { get; set; }
        // some data from profile, like statistics, succeed tests,..
        public string ProfileProperty1 { get; set; }
        public string ProfileProperty2 { get; set; }
        public string ProfileProperty3 { get; set; }

        #endregion

        #region Commands

        public ICommand BeginTest { get; private set; }
        private void ExecuteBeginTest()
        {
            Attempt attempt = new Attempt() { AccountID = _account.Id, TestID = SelectedTest.Id, TestTime = DateTime.Now };
            AttemptRepository.AddAttempt(attempt);

            //Messenger.Default.Send<ViewModelBase, MainViewModel>(new QuestionViewModel());
            Messenger.Default.Send<string, MainViewModel>("QuestionPage");
            Messenger.Default.Send<Attempt, QuestionViewModel>(attempt);    // Send new attempt
            Messenger.Default.Send<Test, QuestionViewModel>(SelectedTest);  // Send Test
        }

        public ICommand GoToTestPage { get; private set; }
        private void ExecuteGoToTestPage()
        {
        }

        public ICommand GoToProfilePage { get; private set; }
        public void ExecuteGoToProfilePage()
        {
            Messenger.Default.Send<string, MainViewModel>("ProfilePage");
            Messenger.Default.Send<Account, ProfileViewModel>(_account);
        } 

        #endregion

        
    }
}